package cn.sytton.taffe.framework.mybatis;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * mybatis拦截器配置
 *
 * @author skyrock
 */
@Configuration
@ConditionalOnBean({SqlSessionFactory.class})
@AutoConfigureAfter({MybatisAutoConfiguration.class})
public class MyBatisUpdateInterceptor implements InitializingBean {

    private final List<SqlSessionFactory> sqlSessionFactoryList;

    public MyBatisUpdateInterceptor(List<SqlSessionFactory> sqlSessionFactoryList) {
        this.sqlSessionFactoryList = sqlSessionFactoryList;
    }

    @Override
    public void afterPropertiesSet() {
        SqlUpdateInterceptor updateInterceptor = new SqlUpdateInterceptor();

        for (SqlSessionFactory sqlSessionFactory : this.sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(updateInterceptor);
        }
    }
}
